﻿using DotNetCore.CAP.Messages;
using Exceptionless;
using Exceptionless.Logging;
using Exceptionless.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation.Log
{
    /// <summary>
    /// 分布式日志类
    /// </summary>
    public class DistributedLog:IDistributedLog
    {
        /// <summary>
        /// 追溯日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Trace(string source, string message, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Trace).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Debug(string source, string message, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Debug).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 信息日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Info(string source, string message, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Info).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 警告日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Warn(string source, string message, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Warn).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Error(string source, string message, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Error).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 致命错误日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Fatal(string source, string message, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Fatal).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 其他日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Other(string source, string message, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Other).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 异常日志
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">日志内容</param>
        /// <param name="data">日志数据</param>
        /// <param name="tags">标签</param>
        public void Exception(string source, Exception exception, object? data, params string[] tags)
        {
            if (!ExceptionlessClient.Default.Configuration.Enabled)
                return;
            var evt = ExceptionlessClient.Default.CreateException(exception).SetSource(source).AddTags(tags).AddTags(tags);
            if (data != null)
            {
                evt.AddObject(data);
            }
            evt.Submit();
        }

        /// <summary>
        /// 创建日志事件处理类（需要手动调用Submit提交日志）
        /// </summary>
        /// <param name="source">模块</param>
        /// <param name="message">消息</param>
        /// <param name="logLevel">日志等级</param>
        /// <returns></returns>
        public EventBuilder CreateLog(string source,string message,LogLevel logLevel)
        {
            return ExceptionlessClient.Default.CreateLog(source, message, logLevel);
        }
    }
}
